﻿using System;
using System.Data;
using System.Text;
using System.Data.OracleClient;
using DBUtility;

namespace OracleDAL.Sec
{
    /// <summary>
    /// 数据访问类SEC_ARMYCODE。
    /// </summary>
    public class SEC_ARMYCODE
    {
        public SEC_ARMYCODE()
        { }
        #region  成员方法


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string ARMYISN)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SEC_ARMYCODE");
            strSql.Append(" where ARMYISN='" + ARMYISN + "' ");
            return OracleHelper.Exists(strSql.ToString());
        }
        public bool ExistsBYwhere(string strWhere)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select count(1) from SEC_ARMYCODE where ");
            sql.Append(strWhere);
            return OracleHelper.Exists(sql.ToString());
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.Sec.SEC_ARMYCODE model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.ARMYISN != null)
            {
                strSql1.Append("ARMYISN,");
                strSql2.Append("'" + model.ARMYISN + "',");
            }
            if (model.ARMYID != null)
            {
                strSql1.Append("ARMYID,");
                strSql2.Append("'" + model.ARMYID + "',");
            }
            if (model.ARMYCODE != null)
            {
                strSql1.Append("ARMYCODE,");
                strSql2.Append("'" + model.ARMYCODE + "',");
            }
            if (model.ARMYDESIGNATOR != null)
            {
                strSql1.Append("ARMYDESIGNATOR,");
                strSql2.Append("'" + model.ARMYDESIGNATOR + "',");
            }
            if (model.NODEINDEX != null)
            {
                strSql1.Append("NODEINDEX,");
                strSql2.Append("'" + model.NODEINDEX + "',");
            }
            if (model.NODEID != null)
            {
                strSql1.Append("NODEID,");
                strSql2.Append("'" + model.NODEID + "',");
            }
            strSql.Append("insert into SEC_ARMYCODE(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int row = OracleHelper.ExecuteSql(strSql.ToString());
            if (row > 0)
            {
                return true;
            }
            else return false;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Sec.SEC_ARMYCODE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SEC_ARMYCODE set ");
            strSql.Append("ARMYID='" + model.ARMYID + "',");
            strSql.Append("ARMYCODE='" + model.ARMYCODE + "',");
            strSql.Append("ARMYDESIGNATOR='" + model.ARMYDESIGNATOR + "',");
            strSql.Append("NODEINDEX='" + model.NODEINDEX + "',");
            strSql.Append("NODEID='" + model.NODEID + "'");
            strSql.Append(" where ARMYISN='" + model.ARMYISN + "' ");
            int row = OracleHelper.ExecuteSql(strSql.ToString());
            if (row > 0)
            {
                return true;
            }
            else return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ARMYISN)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete SEC_ARMYCODE ");
            strSql.Append(" where ARMYISN='" + ARMYISN + "' ");
            int row = OracleHelper.ExecuteSql(strSql.ToString());
            if (row > 0)
            {
                return true;
            }
            else return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Sec.SEC_ARMYCODE GetModel(string ARMYISN)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" ARMYISN,ARMYID,ARMYCODE,ARMYDESIGNATOR,NODEINDEX,NODEID ");
            strSql.Append(" from SEC_ARMYCODE ");
            strSql.Append(" where ARMYISN='" + ARMYISN + "' ");
            Model.Sec.SEC_ARMYCODE model = new Model.Sec.SEC_ARMYCODE();
            DataSet ds = OracleHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.ARMYISN = ds.Tables[0].Rows[0]["ARMYISN"].ToString();
                model.ARMYID = ds.Tables[0].Rows[0]["ARMYID"].ToString();
                model.ARMYCODE = ds.Tables[0].Rows[0]["ARMYCODE"].ToString();
                model.ARMYDESIGNATOR = ds.Tables[0].Rows[0]["ARMYDESIGNATOR"].ToString();
                model.NODEINDEX = ds.Tables[0].Rows[0]["NODEINDEX"].ToString();
                model.NODEID = ds.Tables[0].Rows[0]["NODEID"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ARMYISN,ARMYID,ARMYCODE,ARMYDESIGNATOR,NODEINDEX,NODEID ");
            strSql.Append(" FROM SEC_ARMYCODE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return OracleHelper.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得最大的armyid
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public string GetMaxID(string strWhere)
        {
            StringBuilder sql = new StringBuilder();
            string maxID = string.Empty;
            sql.Append("select max(armyid)   from sec_armycode  where ");
            sql.Append(strWhere);
            OracleDataReader dr = OracleHelper.ExecuteReader(sql.ToString());
            while (dr.Read())
            {
                maxID = dr[0].ToString();
            }
            dr.Close();
            return maxID;
          
        }
        public int GetCount(string strWhere)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from sec_armycode where ");
            sql.Append(strWhere);
            OracleDataReader dr = OracleHelper.ExecuteReader(sql.ToString());
            int i = 0;
            while (dr.Read())
                i++;
            return i;
        }
        public bool MoveNodeIndex(int i, string armyid)
        {
            string sql = "update sec_armycode set nodeindex='" + i + "' where nodeindex='" + i + 1 + "' and armyid like'" + armyid + "__'";
            int rows = OracleHelper.ExecuteSql(sql);
            if (rows > 0)
                return true;
            else
                return false;
        }
        /*
        */

        #endregion  成员方法
    }
}

